package Test02;

import javax.swing.*;
import java.util.Scanner;

public class StudentUI {
    private Scanner sc = new Scanner(System.in);
    private SupperArray array = new SupperArray();

    /**
     * 菜单
     */
    public void menu(){
        JOptionPane.showMessageDialog(null,"欢迎使用学生管理系统");
        while(true){
            String input = JOptionPane.showInputDialog(null,"请选择：\n " +
                    "1-增加 2-修改 3-删除 4-查询 5-退出");
            if(input.equals("1")){
                insert();
            }else if(input.equals("2")){
                update();
            }else if(input.equals("3")){
                delete();
            }else if(input.equals("4")){
                search();
            }else if(input.equals("5")){
                JOptionPane.showMessageDialog(null,"再见");
                System.exit(0);
            }else{

            }
        }

    }

    /**
     *添加学生
     */
    public void insert() {
        String name = JOptionPane.showInputDialog(null, "请输入学生的姓名：");
        if(name == null){
            JOptionPane.showMessageDialog(null,"输入有误");
            return;
        }
        int age = 0;
        try {
            age = Integer.parseInt(JOptionPane.showInputDialog(null, "请输入学生年龄："));
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "年龄输入出错");
            return;
        }
        String gender = JOptionPane.showInputDialog(null, "请输入学生性别");
        if(gender == null){
            JOptionPane.showMessageDialog(null,"输入有误");
            return;
        }
        int confirm = JOptionPane.showConfirmDialog(null,"确认添加该学生？");
        if(confirm == 0){
            array.add(new Student(name,age,gender));
            JOptionPane.showMessageDialog(null,"添加成功");
        }
    }
    /**
     *修改学生属性
     */
    public void update(){
        String name = JOptionPane.showInputDialog(null,"请输入要修改学生的姓名：");
        for(int i = 0;i < array.size();i++){
            if(array.get(i).equals(new Student(name))){
                String input = JOptionPane.showInputDialog(null,"请选择要修改的内容：1-姓名 2-年龄 3-性别");
                Student stu = (Student) array.get(i);
                if(input.equals("1")){
                    String newName = JOptionPane.showInputDialog(null,"请输入修改的新名字：");
                    stu.setName(newName);
                }else if(input.equals("2")){
                    stu.setAge(Integer.parseInt(JOptionPane.showInputDialog(null,"请输入修改的新年龄：")));
                }else if(input.equals("3")){
                    String newGender = JOptionPane.showInputDialog(null,"请输入修改的新性别：");
                    stu.setGender(newGender);
                }else{
                    JOptionPane.showMessageDialog(null,"输入有误");
                    return;
                }
                JOptionPane.showInputDialog(null,"修改完成");
                return;
            }
        }
        JOptionPane.showMessageDialog(null,"查无此人");
    }
    /**
     *删除学生
     */
    public void delete(){
        String name = JOptionPane.showInputDialog(null,"请输入要删除学生的姓名：");
        if (name == null){
            return;
        }
        for(int i = 0;i < array.size();i++){
            if(array.get(i).equals(new Student(name))){
                array.remove(i);
                JOptionPane.showMessageDialog(null,"删除成功");
                return;
            }
        }
        JOptionPane.showMessageDialog(null,"查无此人");
    }
    /**
     *查找学生
     */
    public void search(){
        String input = JOptionPane.showInputDialog(null,"请选择：\n 1-查询所有 2-学生姓名");
        if(input.equals("1")){
            for(int i = 0;i < array.size();i++){
                JOptionPane.showMessageDialog(null,array.get(i));
            }
        }else{
            String name = JOptionPane.showInputDialog(null,"请输入要查询的学生姓名：");
            if (name == null){
                return;
            }
            boolean flag = false;
            for(int i = 0;i < array.size();i++){
                if(array.get(i).equals(new Student(name))){
                    JOptionPane.showMessageDialog(null,array.get(i));
                    flag = true;
                }
            }
            if(!flag){
                JOptionPane.showMessageDialog(null,"查无此人");
            }
        }
    }
}
